﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace logicunivesityinventorystore.Store
{
    public partial class InventoryReportWindow : System.Web.UI.Page
    {
        SqlConnection cn;
        SqlCommand cm;
        SqlDataAdapter da;
        ReportDataset ds;
        SqlCommandBuilder cmb;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["reportType"].ToString().Equals("ReorderReport"))
            reorderReport();
            if (Request.QueryString["reportType"].ToString().Equals("InventoryReport"))
                inventoryReport();
        }
        private void inventoryReport()
        {
            //InventoryStoreDBConnectionString
            string conS = System.Configuration.ConfigurationManager.ConnectionStrings["InventoryStoreData"].ConnectionString;
            cn = new SqlConnection(conS);
            cm = new SqlCommand();
            cm.Connection = cn;
            string selectedDate = Request.QueryString["selectedDate"].ToString();
            cm.CommandText = "SELECT dbo.Item.ItemCode, dbo.StockCard.ItemCode AS Expr1, dbo.StockCard.Date, dbo.StockCard.Balance, dbo.Item.ReorderLevel, dbo.Item.UOM, dbo.Item.Bin, dbo.Item.Description"+
            " FROM dbo.Item INNER JOIN dbo.StockCard ON dbo.Item.ItemCode = dbo.StockCard.ItemCode"+
            " where dbo.StockCard.Date= '"+selectedDate+"'";
            da = new SqlDataAdapter(cm);
            ds = new ReportDataset();
            da.Fill(ds, "InventoryReportView1");
            InventoryCrystalReport rrC = new InventoryCrystalReport();
            rrC.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = rrC;
        }
        private void reorderReport()
        {
            string selectedMonth = Request.QueryString["selectedMonth"].ToString();
            string selectedYear = Request.QueryString["selectedYear"].ToString();
            string conS = System.Configuration.ConfigurationManager.ConnectionStrings["InventoryStoreData"].ConnectionString;
            cn = new SqlConnection(conS);
            cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandText = "SELECT dbo.Item.ItemCode, dbo.Item.Description, dbo.Item.ReorderLevel, dbo.Item.ReorderQty, dbo.Item.LogicStock, dbo.OrderDetails.ItemCode AS Expr1, dbo.OrderDetails.PONo, dbo.Orders.PONo AS Expr2, dbo.Orders.ExpectedDate, dbo.Orders.OrderDate, dbo.Orders.Status" +
                " FROM dbo.Item INNER JOIN dbo.OrderDetails ON dbo.Item.ItemCode = dbo.OrderDetails.ItemCode INNER JOIN dbo.Orders ON dbo.OrderDetails.PONo = dbo.Orders.PONo" +
                " where Month( dbo.Orders.OrderDate)=" + selectedMonth + " and year(dbo.Orders.OrderDate)=" + selectedYear;
            da = new SqlDataAdapter(cm);
            ds = new ReportDataset();
            da.Fill(ds, "ReorderReportView");
            ReorderReportCrystal rrC = new ReorderReportCrystal();
            rrC.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = rrC;
        }
    }
}